fix(RepeatDetection): use stdout+exit 0 to inject warning as additionalContext#1306
Open
J8MAE wants to merge 1 commit into
Open
fix(RepeatDetection): use stdout+exit 0 to inject warning as additionalContext#1306J8MAE wants to merge 1 commit into
J8MAE wants to merge 1 commit into
Conversation
The UserPromptSubmit hook contract is that the hook can return text via stdout + exit 0, which Claude Code injects as `additionalContext` into the model's context. stderr + exit 2 is the BLOCK pattern — it prevents the prompt from being processed and surfaces the message to the user instead of the model. Before this fix, when RepeatDetection triggered: - The user's repeated prompt was blocked - The "⚠️ REPEAT DETECTION" warning surfaced to the user (who already knows they repeated; they're the one repeating) - The model never received the signal it was supposed to act on After this fix: - The user's prompt is allowed to proceed - The warning is injected into the model's context as additionalContext - The model receives the signal to STOP and re-read the user's message Two-line change: stderr→stdout, exit(2)→exit(0). No other behavior changed.
|
Great callout! I opened #1291 for this as well. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
RepeatDetection.hook.ts(UserPromptSubmit hook) writes its warning to stderr and exits with code 2, which is the Claude Code hook block pattern — it prevents the user's prompt from being processed and surfaces the warning text to the user.The desired behavior, per the hook's own docstring ("injects a high-priority WARNING into the model's context"), is the additionalContext pattern: write to stdout and exit with code 0. That signals Claude Code to inject the text into the model's context for the next turn, while letting the prompt proceed.
Bug observed
When the hook triggered on a repeated prompt:
Fix
Two-line change in
Releases/v5.0.0/.claude/hooks/RepeatDetection.hook.ts:process.stderr.write(...)→process.stdout.write(...)process.exit(2)→process.exit(0)Comments updated to reflect the corrected semantics. No other behavior changed; the detection logic, threshold (0.6 jaccard similarity), and warning text are unchanged.
Reference
Claude Code UserPromptSubmit hook contract: stdout + exit 0 =
additionalContextinjected for the model; stderr + exit 2 = block + surface to user. The latter is intended for security/policy violations where the prompt should not proceed.